package hanelsoft.vn.dictionaylearn.control.service;

import hanelsoft.vn.dictionaylearn.model.db.VocabularySQLiteHelper;
import hanelsoft.vn.dictionaylearn.model.entity.InfoVocabularyContent;

import java.util.ArrayList;

import android.app.Service;
import android.appwidget.AppWidgetManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.os.IBinder;
import android.util.Log;

public class WidgetUpdateService extends Service {
	ArrayList<InfoVocabularyContent> array;
	InfoVocabularyContent info;
	VocabularySQLiteHelper mVocabularySQLiteHelper = new VocabularySQLiteHelper(this);
	String TAG = "[WidgetUpdateService]";
	SharedPreferences shareWidget;
	int positionStart;
	@Override
	public IBinder onBind(Intent intent) {
		Log.i(TAG, "onBind Sevice");
		return null;
	}
	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		Log.i(TAG, "onStartCommand Sevice");
		registerReceiver(mReceiver, new IntentFilter(AppWidgetManager.ACTION_APPWIDGET_UPDATE));
		//
		shareWidget = getSharedPreferences("ShareWidget", 0);
		positionStart = shareWidget.getInt("positionStart",0); 
		String name_table_vocabulary = shareWidget.getString("namelist", "");
		int sizeOfListVocabulary = getInfo(name_table_vocabulary).size();
		if(positionStart >= sizeOfListVocabulary)
		{
			positionStart = 0;
		}
		intent = new Intent(WidgetUpdateService.this, WidgetMyProvider.class);
		intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
		info = getInfo(name_table_vocabulary).get(positionStart);
		String word = info.getWord();
		String content = info.getContent();
		shareWidget.edit().putString("WidgetMyWidgetProvider_word", word).commit();
		shareWidget.edit().putString("WidgetMyWidgetProvider_content", content).commit();
		sendBroadcast(intent);
		positionStart= positionStart + 1;
		shareWidget.edit().putInt("positionStart",positionStart).commit();
		//
		//
		return Service.START_NOT_STICKY;
	}
	@Override
	public void onCreate() {
		super.onCreate();
		Log.i(TAG, "Create Sevice");
		registerReceiver(mReceiver, new IntentFilter(AppWidgetManager.ACTION_APPWIDGET_UPDATE));
	}
	BroadcastReceiver mReceiver = new BroadcastReceiver() {
		@Override
		public void onReceive(Context context, Intent intent) {
			Log.i(TAG, "start onReceive");
			if(intent.getAction().equals(AppWidgetManager.ACTION_APPWIDGET_UPDATE))
			{
				intent = new Intent(WidgetUpdateService.this, WidgetMyProvider.class);
				intent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
				shareWidget = context.getSharedPreferences("ShareWidget", 0);
				String name_table_vocabulary = shareWidget.getString("namelist", "");
				info = getInfo(name_table_vocabulary).get(0);
				String word = info.getWord();
				String content = info.getContent();
				shareWidget.edit().putString("WidgetMyWidgetProvider_word", word).commit();
				shareWidget.edit().putString("WidgetMyWidgetProvider_content", content).commit();
				sendBroadcast(intent);
			}
			
		}
	};
	private ArrayList<InfoVocabularyContent> getInfo(String name_table_vocabulary) {
		Log.i(TAG, "Start get info list: "+name_table_vocabulary);
		array = mVocabularySQLiteHelper.viewAllVocabularyContent(name_table_vocabulary);
		Log.i(TAG, "Finish get info list: "+name_table_vocabulary);
		return array;
	}
} 